package com.ym.student.system.controller;

import com.ym.student.system.entity.Teacher;
import com.ym.student.system.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    // 获取所有教师信息
    @GetMapping
    public List<Teacher> getAllTeachers() {
        return teacherService.list();
    }

    // 根据编号获取教师
    @GetMapping("/{id}")
    public Teacher getTeacherById(@PathVariable Integer id) {
        return teacherService.getById(id);
    }

    // 添加教师
    @PostMapping
    public boolean addTeacher(@RequestBody Teacher teacher) {
        return teacherService.save(teacher);
    }

    // 更新教师
    @PutMapping("/{id}")
    public boolean updateTeacher(@PathVariable Integer id, @RequestBody Teacher teacher) {
        teacher.setTeacherNo(id); // 确保更新的是同一个ID
        return teacherService.updateById(teacher);
    }

    // 删除教师
    @DeleteMapping("/{id}")
    public boolean deleteTeacher(@PathVariable Integer id) {
        return teacherService.removeById(id);
    }
    // 在TeacherController.java中添加以下方法
    @GetMapping("/count")
    public ResponseEntity<Map<String, Object>> getTeacherCount() {
        try {
            int count = (int) teacherService.count();
            return ResponseEntity.ok(Map.of(
                    "success", true,
                    "code", 200,
                    "message", "获取教师总数成功",
                    "data", Map.of("count", count)
            ));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of(
                            "success", false,
                            "code", 500,
                            "message", "获取教师总数失败",
                            "error", e.getMessage()
                    ));
        }
    }
}